package br.com.consultoria.andre.biblos.server.services;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.consultoria.andre.biblos.server.web.model.Funcionario;

@Component
public class FuncionarioDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void save(Funcionario func) {
		entityManager.persist(func);
	}

	@SuppressWarnings("unchecked")
	public List<Funcionario> list() {
		List<Funcionario> funcs = new ArrayList<Funcionario>();
		funcs = entityManager.createQuery("select f from funcionario f").getResultList();
		return funcs; 
	}

	@Transactional
	public Boolean excluirPorId(Long id) {
		Boolean retorno = false;

		try {
			Funcionario rmFunc = entityManager.find(Funcionario.class, id);
	        entityManager.remove(rmFunc);
			retorno = true;
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		return retorno;
	}
	
}
